package com.kunya.product.service.impl.specification;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.specification.SpecificationAttributeMapper;
import com.kunya.product.dao.specification.SpecificationMapper;
import com.kunya.product.po.Specification;
import com.kunya.product.po.SpecificationAttribute;
import com.kunya.product.service.specofocation.ISpecificationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 规格业务实现类
 * @author suimg
 * @Description:
 * @param
 * @date 2018/7/10 10:08
 */
@Service("iSpecificationService")
public class SpecificationServiceImpl implements ISpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationAttributeMapper specificationAttributeMapper;

    @Override
    public ServerResponse addnewSpecification(Long categoryId,String name, Byte status, Integer sort, String tablePrefix) {
        Specification specification=new Specification();
        specification.setCategoryId(categoryId);
        specification.setName(name);
        specification.setStatus(status);
        specification.setSort(sort);
        specification.setCreateTime(new Date());
        //TODO 从Shrio当中获取当前登录的用户名
        specification.setCreateBy("Suimg");
        specification.setUpdateTime(new Date(0));
        specification.setUpdateBy(null);
        try{
            specificationMapper.insert(specification,tablePrefix);
        }catch(DuplicateKeyException e){
            return ServerResponse.createByErrorMessage("请不要创建已存在的数据");
        }
        return ServerResponse.createBySuccess("创建规格成功");
    }

    @Override
    public ServerResponse updateSpecification(Specification specification, String tablePrefix){
        if (specification.getSpecificationId()== null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (specification.getName() == null){
            return ServerResponse.createByErrorMessage("name不能为空");
        }
        Specification specification1 = new Specification();
        specification1.setSpecificationId(specification.getSpecificationId());
        specification1.setName(specification.getName());
        specification1.setUpdateBy("hnsj001");
        specification1.setUpdateTime(new Date());
        int num  =  specificationMapper.updateByPrimaryKeySelective(specification1,tablePrefix);
        if (num <= 0){
            return ServerResponse.createByErrorMessage("修改失败!");
        }else{
            return ServerResponse.createBySuccessMessage("修改成功!");
        }



    }

    @Override
    @Transactional
    public ServerResponse deleteSpecification(Specification specification,String tablePrefix){
        if(specification.getSpecificationId() == null){
            return ServerResponse.createByErrorMessage("specificationId不能为空");
        }
        int num = specificationMapper.deleteByPrimaryKey(specification.getSpecificationId(),tablePrefix);
        if (num == 0){
            throw new RuntimeException("删除失败");
        }
        List<SpecificationAttribute> list  = specificationAttributeMapper.selectBySpecificationId(specification.getSpecificationId(),tablePrefix);
        for (SpecificationAttribute specificationAttribute:list){
            Long specAttrId = specificationAttribute.getSpecAttrId();
            int number = specificationAttributeMapper.deleteByPrimaryKey(specAttrId,tablePrefix);
            if (number <= 0){
                throw new RuntimeException("删除失败");
            }
        }

        return ServerResponse.createByErrorMessage("删除成功");
    }

    @Override
    public ServerResponse seleteSpecification(Integer pageNum,Integer pageSize,String tablePrefix){
        if (pageNum == null){
            return ServerResponse.createBySuccessMessage("pageNum不能为空");
        }
        if (pageSize == null){
            return ServerResponse.createByErrorMessage("pageSize不能为空");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String,Object>> list = specificationMapper.seleteSpecification(tablePrefix);
        if (list == null){
            return ServerResponse.createByErrorMessage("查询失败");
        }
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
        return ServerResponse.createBySuccess(pageInfo);
    }

}
